# Author: Mark Richardson
# Purpose: Create Figure H1

# Load packages
library(dplyr)
library(ggplot2)
library(readr)
library(patchwork)

# Load data
dis <- read_csv(file = "data/01_dis_data.csv")
perf <- load("data/07_performance_ratings_all_fmtd.RData")

# Identify non-matches
x <- anti_join(dis, perf_ratings, by = "agency")

# Adjust agency names
perf_ratings <- perf_ratings %>%
  mutate(agency = case_when(agency == "Citizenship and Immigration Services" ~ "United States Citizenship and Immigration Services",
                            agency == "Customs and Border Protection" ~ "United States Customs and Border Protection",
                            TRUE ~ agency))

perf_dis <- full_join(dis, perf_ratings, by = "agency")

## Create the figure

p1 <- ggplot(perf_dis, aes(pty_dis, perf_rating_inf_hier)) +
  geom_point(alpha = 0.75) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Partisan Disagreement",
       y = "Performance Rating")

p2 <- ggplot(perf_dis, aes(pty_dis, perf_rating_inf_hier_sd)) +
  geom_point(alpha = 0.75) +
  geom_smooth(method = "lm") +
  ylim(0, 1) +
  theme_bw() +
  labs(x = "Partisan Disagreement",
       y = "Std. Dev. of Performance Rating")

p <- p1 / p2

ggsave("figs/fgH1.png", device = "png", dpi = "retina",
       units = "in", width = 7, height = 10)

summary(lm(perf_rating_inf_hier_sd ~ pty_dis, data = perf_dis))
